
package com.src.ptit.javatraining.huynh.btvn2;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

/**
 *
 * @author user
 */
public class Bai1 {

    public static void main(String[] args) {
        long a, a1;
        try {
            FileReader f = new FileReader("Bai1.txt");
            BufferedReader fin = new BufferedReader(f);
            String s;
            s = fin.readLine();
            if (s.length() < 1) {
                throw new NullPointerException();
            }
            String[] s2 = s.split(" ");
            a = Long.parseLong(s2[0], 10);
            a1 = Long.parseLong(s2[1]);
            if (a < 0) {
                throw new Exception("Number <0 can't be converted");
            }
            String b;
            File f3 = new File("Bai1OUT.txt");
            f3.createNewFile();
            FileWriter fout = new FileWriter(f3);
            try {
                BufferedWriter f1 = new BufferedWriter(fout);
                if (a1 == 2) {
                    b = Long.toBinaryString(a);
                    
                    f1.write(s2[0] + " in " + s2[1] + " is " + b);
                    f1.newLine();
                } else if (a1 == 8) {
                    b = Long.toOctalString(a);
                    f1.write(s2[0] + " in " + s2[1] + " is " + b);
                    f1.newLine();
                } else if (a1 == 16) {
                    b = Long.toHexString(a);
                    f1.write(s2[0] + " in " + s2[1] + " is " + b);
                    f1.newLine();
                } else {
                    throw new Exception("Not a  number system !");
                }
                f1.close();
            } catch (Exception e) {
                System.out.println("Can't open file !");
            } 
            
         } catch (Exception e) {
            System.out.println("ERROR " + e);
        }
    }
}
